// 判断是否为邮箱格式
export function isEmail(str: string) {
  var regu =
    "^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|com|gov|mil|org|edu|int|name|asia)$";
  var re = new RegExp(regu);
  if (str.search(re) == -1) {
    return false;
  } else {
    return true;
  }
}

// 判断是否含有大写字母
export function hasCapital(str: string) {
  var result = str.match(/^.*[A-Z]+.*$/);
  if (result == null) return false;
  return true;
}

// 判断是否含有小写字母
export function hasLowercase(str: string) {
  var result = str.match(/^.*[a-z]+.*$/);
  if (result == null) return false;
  return true;
}

// 判断是否含有数字
export function hasNumber(str: string) {
  var result = str.match(/^.*[0-9]+.*$/);
  if (result == null) return false;
  return true;
}

// 判断是否含有其他字符
export function hasOther(str: string) {
  var result = str.match(/^.*[^0-9A-Za-z]+.*$/);
  if (result == null) return false;
  return true;
}

// 判断是否含有字母
export function hasLetter(str: string) {
  var result = str.match(/^.*[A-Za-z]+.*$/);
  if (result == null) return false;
  return true;
}
// 8~13位,包含数字跟大小字母
export function isPassword(val: string) {
  if (/^(?![0-9]+$)(?![A-Z]+$)(?![a-z]+$)[0-9A-Za-z]{8,13}$/.test(val)) {
    return true;
  }
  return false;
}

export let password_validate = ({ inputValidators, index }: any) => {
  return async (rule: any, value: string) => {
    if (isEmptyType(value)) {
      inputValidators[index] = true;
      return Promise.reject($t("placeholder16"));
    }
    if (isPassword(value)) {
      inputValidators[index] = false;
      return Promise.resolve();
    } else {
      inputValidators[index] = true;
      return Promise.reject($t("placeholder6") + "8-13" + $t("placeholder104"));
    }
  };
};

export let password_new_confirm_validate = ({
  formState,
  keyArray = ["password_new_confirmation"],
  inputValidators,
  index,
  callback,
}: any) => {
  return async (rule: any, value: string) => {
    if (isEmptyType(value)) {
      inputValidators[index] = true;
      return Promise.reject($t("placeholder0"));
    }

    if (isPassword(value) && value === formState[keyArray[0]]) {
      callback && callback();
      return Promise.resolve();
    }

    if (
      !isEmptyType(formState[keyArray[0]]) &&
      value !== formState[keyArray[0]]
    ) {
      inputValidators[index] = true;
      return Promise.reject($t("placeholder13"));
    }

    if (isPassword(value)) {
      inputValidators[index] = false;
      return Promise.resolve();
    } else {
      inputValidators[index] = true;
      return Promise.reject($t("placeholder6") + "8-13" + $t("placeholder104"));
    }
  };
};

export let password_new_validate = ({
  formState,
  inputValidators,
  index,
  keyArray = ["password_new"],
  callback,
}: any) => {
  return async (rule: any, value: string) => {
    if (isEmptyType(value)) {
      inputValidators[index] = true;
      return Promise.reject($t("placeholder14"));
    }

    if (
      !isEmptyType(formState[keyArray[0]]) &&
      value !== formState[keyArray[0]]
    ) {
      inputValidators[index] = true;
      return Promise.reject($t("placeholder13"));
    }

    if (
      isPassword(value) &&
      !isEmptyType(formState[keyArray[0]]) &&
      value === formState[keyArray[0]]
    ) {
      callback && callback();
      return Promise.resolve();
    }

    if (isPassword(value)) {
      inputValidators[index] = false;
      return Promise.resolve();
    } else {
      inputValidators[index] = true;
      return Promise.reject($t("placeholder6") + "8-13" + $t("placeholder104"));
    }
  };
};

// 数字
export function onlyNum(input) {
  input.value = input.value?.replace(/\D+/g, "");
}
// 整数(0+正整数)
export function onlyInt(input) {
  let value = input.value;
  value = value?.replace(/\D+/g, "");
  input.value = value ? Number(value).toString() : value; // 去掉开头多个0
}
// 正整数
export function onlyIntp(input) {
  if (!/^[1-9][0-9]*$/.test(input.value)) {
    let value = input.value?.replace(/\D+/g, "");
    if (value && value.substring(0, 1) === "0") {
      // 0开头去除0
      value = value.substring(1);
    }
    input.value = value;
  }
}

// 数字+小数点
export function onlyNumPoint(input) {
  input.value = input.value?.replace(/[^\d.]/g, "");
}

// 浮点型
// eslint-disable-next-line no-unused-vars
export function onlyFloat(input, n) {
  let value = input.value;
  value = value?.replace(/[^\d.]/g, "");
  value = value?.replace(/^\./g, "");
  value = value?.replace(".", "$#$")?.replace(/\./g, "")?.replace("$#$", ".");
  if (n && Number(n) > 0) {
    // 限制小数点后n位
    var d = new Array(Number(n)).fill(`\\d`).join("");
    // eslint-disable-next-line no-useless-escape
    var reg = new RegExp(`^(\\-)*(\\d+)\\.(${d}).*$`, "ig");
    value = value?.replace(reg, "$1$2.$3");
  }
  if (value && !value.includes(".")) {
    value = Number(value).toString(); // 去掉开头多个0
  }
  input.value = value;
}
// 字母
export function onlyAlp(input) {
  input.value = input.value?.replace(/[^A-Za-z]/g, "");
}
// 数字+字母
export function onlyNumAlp(input) {
  input.value = input.value?.replace(/[^A-Za-z0-9]/g, "");
}

// 四则运算+-*/()数字
export function onlyArith(input) {
  let value = input.value;
  if (value) {
    input.value = value.split("").reduce((prev, cur) => {
      // eslint-disable-next-line no-useless-escape
      if (/^[\d|\-|\+|\*|\/|\.|\(|\)]+$/.test(cur)) {
        return prev + cur;
      }
      return prev;
    }, "");
  }
}

// 11位数字
export function onlyPhone(input) {
  let value = input.value;
  value = value?.replace(/\D+/g, "");
  input.value = value ? Number(value.substring(0, 11)) : value;
}

// 去除两头空格
export function onlyTrim(input) {
  let value = input.value;
  value = value?.replace(/^\s+|\s+$/g, "");
  input.value = value;
}
// 替换特殊符号为空字符串
export function only_limit_charts(input) {
  let value = input.value;
  value = value?.replace(/[^a-zA-Z0-9_/\-\\.\s:?\u4e00-\u9fa5]/g, "");
  input.value = value;
}

export function onlyRemoveTextAreaSpecialChars(input) {
  let pattern = new RegExp(
    "[`~%!@#^=''?~！@#￥……&——‘”“'？*()（），,。.、]",
    "g"
  );
  let value = input.value;
  value = value?.replace(pattern, "");
  input.value = value;
}
